package com.insigma.business.qrcode.appmgt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.qrcode.appmgt.dao.CommonCodeappRemovedDao;
import com.insigma.business.qrcode.appmgt.dto.CommonCodeappRemovedDTO;
import com.insigma.business.qrcode.appmgt.dto.request.CommonCodeappRemovedInsertRequest;
import com.insigma.business.qrcode.appmgt.dto.request.CommonCodeappRemovedUpdateRequest;
import com.insigma.business.qrcode.appmgt.dto.search.CommonCodeappRemovedSearch;
import com.insigma.business.qrcode.appmgt.entity.CommonCodeappRemovedEntity;
import com.insigma.business.qrcode.appmgt.dto.request.CommonCodeappRemovedSearchRequest;
import com.insigma.business.qrcode.appmgt.service.CommonCodeappRemovedService;
import com.insigma.business.qrcode.common.converter.CommonCodeappRemovedProviderConverter;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.BizException;
import com.insigma.business.qrcode.common.util.DateUtils;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("commonCodeappRemovedService")
public class CommonCodeappRemovedServiceImpl extends BaseProviderImpl implements CommonCodeappRemovedService {
    @Resource
    private CommonCodeappRemovedDao commonCodeappRemovedDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Override
    public BasePagingResponse<List<CommonCodeappRemovedDTO>> searchAll(CommonCodeappRemovedSearchRequest searchRequest) {
        final BasePagingResponse<List<CommonCodeappRemovedDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonCodeappRemoved的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonCodeappRemovedSearch commonCodeappRemovedSearch = CommonCodeappRemovedProviderConverter.convertRequestSO2Searcher(searchRequest);
                PageHelper.startPage(commonCodeappRemovedSearch.getPageNo(), commonCodeappRemovedSearch.getPageSize());
                final List<CommonCodeappRemovedEntity> commonCodeappRemovedEntityList = commonCodeappRemovedDao.searchAll(commonCodeappRemovedSearch);
                PageInfo<CommonCodeappRemovedEntity> pageinfo = new PageInfo<>(commonCodeappRemovedEntityList);
                final List<CommonCodeappRemovedDTO> commonCodeappRemovedDTOList = CommonCodeappRemovedProviderConverter.convertResponseDOList2DTOList(commonCodeappRemovedEntityList);
                for (CommonCodeappRemovedDTO commonCodeappRemovedDTO : commonCodeappRemovedDTOList) {
                    commonCodeappRemovedDTO.setCreateDateTime(commonCodeappRemovedDTO.getCreateDatetime());
                }
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, commonCodeappRemovedDTOList, searchRequest,pageinfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_CODEAPP_REMOVED_SEARCH_ALL_DATA;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BaseResponse<List<CommonCodeappRemovedDTO>> searchEnable() {
        final BaseResponse<List<CommonCodeappRemovedDTO>> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {}

            @Override
            public void executeService() {
                CommonCodeappRemovedSearchRequest searchRequest = new CommonCodeappRemovedSearchRequest();
                searchRequest.setAppStatus(1);
                final CommonCodeappRemovedSearch commonCodeappRemovedSearch = CommonCodeappRemovedProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<CommonCodeappRemovedEntity> commonCodeappRemovedEntityList = commonCodeappRemovedDao.searchAll(commonCodeappRemovedSearch);
                final List<CommonCodeappRemovedDTO> commonCodeappRemovedDTOList = CommonCodeappRemovedProviderConverter.convertResponseDOList2DTOList(commonCodeappRemovedEntityList);
                for (CommonCodeappRemovedDTO commonCodeappRemovedDTO : commonCodeappRemovedDTOList) {
                    commonCodeappRemovedDTO.setCreateDateTime(commonCodeappRemovedDTO.getCreateDatetime());
                }
                ResponseUtils.buildSuccessResponse(baseResponse,commonCodeappRemovedDTOList);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_CODEAPP_REMOVED_SEARCH_ENABLE_DATA;
            }
        });

        return baseResponse;
    }

    @Override
    public BaseResponse<String> dataInsert(CommonCodeappRemovedInsertRequest insertRequest) {
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonCodeappRemoved的请求参数不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getAppName(),"应用名称不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getAppUrl(),"链接地址不能为空");
                BizCheckUtil.checkNotBlank(insertRequest.getAppImg(),"功能图标不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getAppSort(),"排序不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getAppStatus(),"状态不能为空");

                final List<CommonCodeappRemovedEntity> commonCodeappRemovedEntityList = commonCodeappRemovedDao.selectAll(new CommonCodeappRemovedEntity());
                List<CommonCodeappRemovedEntity> collect = commonCodeappRemovedEntityList.stream().filter(o -> o.getAppSort() == insertRequest.getAppSort()).collect(Collectors.toList());
                if(ObjectUtil.isNotEmpty(collect)){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "该序号已存在，请修改");
                }
            }
            @Override
            public void executeService() {
                CommonCodeappRemovedEntity commonCodeappRemovedEntity = CommonCodeappRemovedProviderConverter.convertRequestIO2DO(insertRequest);
                commonCodeappRemovedEntity.setAppId(sequenceGenerator.genKey(SequenceNameConstant.COMMON_CODEAPP_REMOVED_APP_ID));
                commonCodeappRemovedEntity.setIsDeleted(0);
                commonCodeappRemovedEntity.setCreateDateTime(new Date());
                commonCodeappRemovedDao.insert(commonCodeappRemovedEntity);
                ResponseUtils.setupSuccessResponse(baseResponse, commonCodeappRemovedEntity.getAppId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_CODEAPP_REMOVED_SEARCH_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<CommonCodeappRemovedDTO> dataView(String appId) {
        CommonCodeappRemovedEntity commonCodeappRemovedEntity = commonCodeappRemovedDao.selectByPrimaryKey(appId);
        CommonCodeappRemovedDTO commonCodeappRemovedDTO = CommonCodeappRemovedProviderConverter.convertResponseDO2DTO(commonCodeappRemovedEntity);
        return ResponseUtils.buildSuccessResponse(commonCodeappRemovedDTO);
    }

    @Override
    public BaseResponse<String> dataUpdate(CommonCodeappRemovedUpdateRequest updateRequest) {
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改TrainingVideo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getAppId(),"主键ID不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getAppName(),"应用名称不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getAppUrl(),"链接地址不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getAppImg(),"功能图标不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getAppSort(),"排序不能为空");
                BizCheckUtil.checkNotNull(updateRequest.getAppStatus(),"状态不能为空");

                final List<CommonCodeappRemovedEntity> commonCodeappRemovedEntityList = commonCodeappRemovedDao.selectAll(new CommonCodeappRemovedEntity());
                List<CommonCodeappRemovedEntity> collect = commonCodeappRemovedEntityList.stream().filter(o -> !o.getAppId().equals(updateRequest.getAppId()) && o.getAppSort() == updateRequest.getAppSort()).collect(Collectors.toList());
                if(ObjectUtil.isNotEmpty(collect)){
                    throw new BizException(ErrorCodeEnum.COMMON_INNER_ERROR,  "该序号已存在，请修改");
                }
            }
            @Override
            public void executeService() {
                CommonCodeappRemovedEntity commonCodeappRemovedEntity = CommonCodeappRemovedProviderConverter.convertRequestUO2DO(updateRequest);
                commonCodeappRemovedEntity.setCreateDateTime(new Date());
                commonCodeappRemovedDao.updateByPrimaryKey(commonCodeappRemovedEntity);
                ResponseUtils.setupSuccessResponse(baseResponse, commonCodeappRemovedEntity.getAppId());
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_CODEAPP_REMOVED_SEARCH_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> enableUpdate(String appId, String appStatus) {
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotBlank(appId,"主键ID不能为空");
                BizCheckUtil.checkNotNull(appStatus,"状态不能为空");
            }
            @Override
            public void executeService() {
                commonCodeappRemovedDao.updateAppStatusByPrimaryKey(appId,appStatus);
                ResponseUtils.setupSuccessResponse(baseResponse, appId);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_CODEAPP_REMOVED_SEARCH_ENABLE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> dataDelete(String appId) {
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotBlank(appId,"主键ID不能为空");
            }
            @Override
            public void executeService() {
                commonCodeappRemovedDao.updateDeleteStatusByPrimaryKey(appId);
                ResponseUtils.setupSuccessResponse(baseResponse, appId);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_CODEAPP_REMOVED_SEARCH_DELETE;
            }
        });
        return baseResponse;
    }
}
